import React, { Component } from 'react'
import "../assert/css/index.css"
import logo from "../assert/image/logo.png"
import yh from "../assert/image/yh.png"
import yh2 from "../assert/image/yh2.png"
import xian from "../assert/image/seckill.png"
import chang from "../assert/image/top.png"
import pin from "../assert/image/brand.png"
import xiao from "../assert/image/selfsupport.png"
import ji from "../assert/image/integral.png"
import shuang from "../assert/image/11.png"
import chang1 from "../assert/image/cg1.png"
import chang2 from "../assert/image/cg2.png"
import { news, wanren, seckill, indexgoods } from '../request/home_req'

import { cartadd } from '../request/cart_request'

export default class home extends Component {
    state = {
        new_list: [],
        wanren_list: [],
        seckill_list: [],
        indexgoods_list: [],
        goods_list: [{
            description: "",
            goodsname: "",
            id: 1,
            img: "",
            market_price: 5.99,
            price: 2.49
        }],
        index_list: 0
    }
    componentWillUnmount() {
        this.setState = () => false //重置了setState
    }
    get_index(index) {
        indexgoods(index).then(res => {
            this.setState({ goods_list: res.list[index].content })
            this.setState({ index_list: index })
        })

    }
    UNSAFE_componentWillMount() {
        news().then(res => {
            this.setState({ new_list: res.list })
        })
        wanren().then(res => {
            this.setState({ wanren_list: res.list })
        })
        seckill().then(res => {
            // console.log(res.list);
            this.setState({ seckill_list: res.list })
        })
        this.get_index(0)
    }

    add(goodsid, type ,num) {
        cartadd(goodsid, type, num).then(res => {
            if (res.code === 200) {
                this.props.getcartlist1()
            }
        })
    }

    render() {
        return (
            <>
                <div className="header">
                    <img src={logo} alt="" />
                    <form action="">
                        <input type="text" placeholder="搜索商品" />
                        <button>搜索</button>
                    </form>
                </div>

                <div className="middle">
                    <div className="xrzx">
                        <div className="clearfix top">
                            <h2 className="ft">新人专享</h2>
                            <a href="/#" className="rt">查看全部&gt;</a>
                        </div>
                    </div>

                    <div className="xrzx1">
                        <p>
                            <span>1重礼</span>
                            <span>新人特价商品专区</span>
                            <span>（限量供应）</span>
                        </p>

                        <div className="look">
                            <ul>
                                {
                                    this.state.new_list.map(item => {
                                        return <li key={item.id}>
                                            <img src={this.pic_path + item.img} alt="" />
                                            <span className="now">￥{item.price}</span>
                                            <span className="pri">￥{item.market_price}</span>
                                            <a href="/#"></a>
                                        </li>
                                    })
                                }


                            </ul>
                        </div>

                    </div>
                    <div className="xrzx2">
                        <p>
                            <span>2重礼</span>
                            <span>新人通用50元礼券（下单立减，省了又省）</span>
                        </p>
                        <div>
                            <img className="yh1" src={yh} alt="" />
                            <img className="yh2" src={yh2} alt="" />
                        </div>
                    </div>
                </div>
                <div className="middle-nav box">
                    <ul>
                        <li>
                            <a href="">
                                <img src={xian} alt="" />
                                <p>限时秒杀</p>
                            </a>
                        </li>
                        <li>
                            <a href="">
                                <img src={chang} alt="" />
                                <p>畅销商品</p>
                            </a>
                        </li>
                        <li>
                            <a href="">
                                <img src={pin} alt="" />
                                <p>品质大牌</p>
                            </a>
                        </li>
                        <li>
                            <a href="">
                                <img src={xiao} alt="" />
                                <p>小u自营</p>
                            </a>
                        </li>
                        <li>
                            <a href="">
                                <img src={ji} alt="" />
                                <p>积分商城</p>
                            </a>
                        </li>
                    </ul>
                </div>

                <div className="ms">
                    <div className="cjms">
                        <div className="top">
                            <h2 className="ft">万人团</h2>
                        </div>
                        {
                            this.state.wanren_list.map(item => (
                                <div className="content" key={item.id}>
                                    <img src={this.pic_path + item.img} alt="" />
                                    <div>
                                        <span>7.9折</span>
                                        <p>{item.goodsname}......</p>

                                        <p onClick={() => {
                                            this.add(item.id,2,1)
                                        }}><b>￥{item.price}</b><u>￥{item.market_price}</u></p>
                                    </div>
                                </div>
                            ))
                        }


                    </div>


                    <div className="xsms">
                        <p>
                            <span>限时秒杀</span>
                            <span>新人特价商品专区</span>
                            <span>（限量供应）</span>
                        </p>
                        <div className="look">
                            <ul>
                                {
                                    this.state.seckill_list.map(item => (
                                        <li key={item.id}>
                                            <img src={this.pic_path + item.img} alt="" />
                                            <span className="now">￥{item.price}</span>
                                            <span className="pri">￥{item.market_price}</span>
                                            <a href="/#"></a>
                                        </li>
                                    ))
                                }


                            </ul>
                        </div>
                    </div>
                </div>
                <div className="two11">
                    <div className="one">
                        <h3>双11尖货预约</h3>
                        <img src={shuang} alt="" />
                    </div>
                    <div className="three">
                        <h3>畅购全球</h3>
                        <img src={chang1} alt="" />
                        <img src={chang2} alt="" />
                    </div>
                </div>

                <div className='nav'>
                    <ul>
                        <li onClick={() => this.get_index(0)} className={this.state.index_list == 0 ? 'active' : ''}>
                            <p>热销好货</p>
                            <span>限量抢购</span>
                        </li>
                        <li onClick={() => this.get_index(1)} className={this.state.index_list == 1 ? 'active' : ''}>
                            <p>折上折区</p>
                            <span>不买就亏</span>
                        </li>
                        <li onClick={() => this.get_index(2)} className={this.state.index_list == 2 ? 'active' : ''}>
                            <p>时令水果</p>
                            <span>当季限定</span>
                        </li>
                        <li onClick={() => this.get_index(3)} className={this.state.index_list == 3 ? 'active' : ''}>
                            <p>粮油调味</p>
                            <span>一键购齐</span>
                        </li>
                    </ul>
                </div>
                <div className="main">
                    {
                        this.state.goods_list.map(item => (
                            <div className="item clearfix" key={item.id}>
                                <div className="pic ft">
                                    <img src={this.pic_path + item.img} alt="" />
                                </div>
                                <div className="intro ft">
                                    <span>{(item.price / item.market_price * 10).toFixed(1)}折</span>
                                    <p>{item.goodsname}......</p>

                                    <u>￥{item.price}</u>
                                    <u>￥{item.market_price}</u>
                                    <button onClick={() => this.add(item.id,1,1)}>加入购物车</button>
                                </div>
                            </div>
                        ))
                    }


                </div>
            </>
        )
    }
}
